package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.slice.slice.slice.SearchAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.sensor.bean.CategoryOther;

import java.util.ArrayList;
import java.util.List;

public class APP_PageAbilitySlice extends AbilitySlice implements Component.ClickedListener{

    Button add;
    Button other2;
    Button other;

    Button voice;

    Button find;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_APP_ability_main1);


        add = (Button) findComponentById(ResourceTable.Id_add);
        add.setClickedListener(this);
        other = (Button) findComponentById(ResourceTable.Id_other);
        other.setClickedListener(this);
        other2 = (Button) findComponentById(ResourceTable.Id_other2) ;
        other2.setClickedListener(this);
        voice = (Button) findComponentById(ResourceTable.Id_voice) ;
        voice.setClickedListener(this);
        find = (Button) findComponentById(ResourceTable.Id_find) ;
        find.setClickedListener(this);

        }



    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    @Override
    public void onClick(Component component) {
        if(component == add){
            Intent c = new Intent();
            //意图中包含了要跳转的页面信息
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")//要跳转到哪个设备上，如果传递一个没有内容的字符串，表示跳转本机
                    .withBundleName("com.example.myapplication")//要跳转到哪个应用上，小括号里面可以写包名
                    .withAbilityName("com.example.myapplication.slice.coldPageAbility")//要跳转的页面
                    .build();//将上面的三个信息进行打包到一个operation对象当中（点击build，按ctrl+Alt+v）
            c.setOperation(operation);//把打包之后的operation设置到意图里
            startAbility(c);//跳转页面
        }else if(component == other2 || component == other){
            Intent c = new Intent();
            //意图中包含了要跳转的页面信息
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")//要跳转到哪个设备上，如果传递一个没有内容的字符串，表示跳转本机
                    .withBundleName("com.example.myapplication")//要跳转到哪个应用上，小括号里面可以写包名
                    .withAbilityName("com.example.myapplication.slice.slice.slice.resourcePageAbility")//要跳转的页面
                    .build();//将上面的三个信息进行打包到一个operation对象当中（点击build，按ctrl+Alt+v）
            c.setOperation(operation);//把打包之后的operation设置到意图里
            startAbility(c);//跳转页面
        }
        if(component == voice){
            Intent c = new Intent();
            //意图中包含了要跳转的页面信息
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")//要跳转到哪个设备上，如果传递一个没有内容的字符串，表示跳转本机
                    .withBundleName("com.example.myapplication")//要跳转到哪个应用上，小括号里面可以写包名
                    .withAbilityName("com.example.myapplication.slice.voiceLookupPageAbility")//要跳转的页面
                    .build();//将上面的三个信息进行打包到一个operation对象当中（点击build，按ctrl+Alt+v）
            c.setOperation(operation);//把打包之后的operation设置到意图里
            startAbility(c);//跳转页面
        }
        if(component == find){
            Intent c = new Intent();
            //意图中包含了要跳转的页面信息
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")//要跳转到哪个设备上，如果传递一个没有内容的字符串，表示跳转本机
                    .withBundleName("com.example.myapplication")//要跳转到哪个应用上，小括号里面可以写包名
                    .withAbilityName("com.example.myapplication.slice.slice.SearchAbility")//要跳转的页面
                    .build();//将上面的三个信息进行打包到一个operation对象当中（点击build，按ctrl+Alt+v）
            c.setOperation(operation);//把打包之后的operation设置到意图里
            startAbility(c);//跳转页面
        }
    }


    private void initIndex(){
        TextField searchTextField =(TextField) findComponentById(ResourceTable.Id_index_search_textField);
        searchTextField.setFocusChangedListener((component,b)->{
            //当搜索输入框获得焦点，导航到SearchAbilitySlice
            if(b){
                present(new SearchAbilitySlice(),new Intent());
            }
        });



    }
}

